#include "thread_control.h"

thread_init_flag_t thread_init_flag_s = {0};
pwm_task_ccr_t pwm_task_ccr_s = {0};

osThreadId ledTaskHandle;
osThreadId pwmTaskHandle;
osThreadId uartTaskHandle;

void menu_init(void)
{
  menu_p0_init();
  menu_p1_init();
  menu_p2_init();
  menu_p3_init();
  menu_p4_init();
  menu_p5_init();
}

void menu_p0_init(void)
{
  add_subpage(&p0, "thread_LED", &p1);
  add_subpage(&p0, "thread_PWM", &p2);
  add_subpage(&p0, "thread_UART", &p3);
  add_subpage(&p0, "thread_4", &p4);

	MenuInit(&p0);
}

void menu_p1_init(void)
{
	add_func(&p1, "<LED_open>", LED_open);
  add_func(&p1, "<LED_close>", LED_close);
}

void menu_p2_init(void)
{
	add_func(&p2, "<pwmOpen>", pwmOpen);
  add_func(&p2, "<pwmClose>", pwmClose);
	add_value(&p2, "[CCR1]", (int *)&pwm_task_ccr_s.ccr1, 1, NULL);
	add_value(&p2, "[CCR2]", (int *)&pwm_task_ccr_s.ccr2, 1, NULL);
	add_value(&p2, "[CCR3]", (int *)&pwm_task_ccr_s.ccr3, 1, NULL);
	add_value(&p2, "[CCR4]", (int *)&pwm_task_ccr_s.ccr4, 1, NULL);
}

void menu_p3_init(void)
{
	add_func(&p3, "<uartOpen>", uartOpen);
  add_func(&p3, "<uartClose>", uartClose);
}

void menu_p4_init(void)
{
	//add_value(&p0, "[EC_value]", (int *)&encoder_s.key, 1, NULL);
}

void menu_p5_init(void)
{
	
}

void LED_open(void)
{
	if(!thread_init_flag_s.task1)
	{
		thread_init_flag_s.task1 = 1;
		/* definition and creation of myTask02 */
		osThreadDef(myTask02, ledTask, osPriorityIdle, 0, 128);
		ledTaskHandle = osThreadCreate(osThread(myTask02), NULL);
	}
	else
	{
		vTaskResume(ledTaskHandle);
	}
}

void LED_close(void)
{
	vTaskSuspend(ledTaskHandle);
	HAL_GPIO_DeInit(GPIOC,13);
}

void pwmOpen(void)
{
	if(!thread_init_flag_s.task2)
	{
		thread_init_flag_s.task2 = 1;
		/* definition and creation of pwmTask */
		osThreadDef(myTask03, pwmTask, osPriorityIdle, 0, 128);
		pwmTaskHandle = osThreadCreate(osThread(myTask03), NULL);
	}
	else
	{
		vTaskResume(pwmTaskHandle);
	}
}

void pwmClose(void)
{
	vTaskSuspend(pwmTaskHandle);
}

void uartOpen(void)
{
	if(!thread_init_flag_s.task2)
	{
		thread_init_flag_s.task2 = 1;
		/* definition and creation of pwmTask */
		osThreadDef(myTask03, uartTask, osPriorityIdle, 0, 128);
		uartTaskHandle = osThreadCreate(osThread(myTask03), NULL);
	}
	else
	{
		vTaskResume(uartTaskHandle);
	}
}

void uartClose(void)
{
	vTaskSuspend(uartTaskHandle);
}
